package TypeWriter;

import java.awt.Color;
import java.awt.Font;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Scanner;

import javax.swing.JColorChooser;

import Actions.HelpDialog;
import Actions.OptionChooser;

public class Options {

	// foreground/background color
	public Color foreground;
	public Color background;
	// font family/size, etc
	public Font font;
	
	// booleans, hardcore mode, show status, etc
	public boolean hardcore;
	public boolean typewriterScroll; // will the typing position be fixed?
	public boolean soundOn;
	
	public boolean startFullScreen;
	
	public int startX;
	public int startY;
	public int startW;
	public int startH;
	
	private String profile;
	
	public String lastFile;
	
	private OptionChooser dialog;
	private HelpDialog help;
	
	private TypeWriter parent;
	
	
	public Options( TypeWriter parent ){
		this.parent = parent;
		
		// start defaults
		foreground = Color.GREEN;
		background = Color.BLACK;
		font = new Font("Courier",Font.PLAIN,14);
		hardcore = true;
		typewriterScroll = true;
		
		startFullScreen = true;
		soundOn = false;
		
		dialog = new OptionChooser(parent);
		help = new HelpDialog(parent);
		
		lastFile = "null";
		
		startX = 600;
		startY = 400;
	}
	
	public void showOptionDialog()
	{
		dialog.show();
		typewriterScroll = dialog.getScrollState();
		soundOn = dialog.getSoundState();
	}
	public void showHelpDialog()
	{
		help.show();
	}
	
	public void chooseForeground()
	{
		setForeground(JColorChooser.showDialog(null, "Choose font color", foreground));
	}
	
	public void chooseBackground()
	{
		setBackground(JColorChooser.showDialog(null, "Choose background color", background));
	}
	
	public void chooseFont()
	{
		Actions.FontChooser chooser = new Actions.FontChooser();
		chooser.setSelectedFont(font);
		if(chooser.showDialog(parent) == Actions.FontChooser.OK_OPTION)
		{
			parent.getEditor().setFont(chooser.getSelectedFont());
		}
	}
	
	public void setFont(Font newFont)
	{
		if( newFont == null ) return;
		font = newFont;
		parent.getEditor().setFont(font);
	}
	
	public void setBackground(Color color)
	{
		if( color==null ) return;
		background = color;
		parent.getEditor().setBackground(background);
	}
	
	public void setForeground(Color color)
	{
		if( color==null ) return;
		foreground = color;
		parent.getEditor().setForeground(foreground);
		parent.getEditor().setCaretColor(foreground);
	}
		
	public void readArgs(String[] args)
	{
		boolean canReadLast = true;
		if( args.length > 0 )
		{
			for( String arg: args)
			{
				if(arg.equals("-hardcoreoff")) hardcore = false;
				else if(arg.equals("-hardcoreon")) hardcore = true;
				else if(arg.equals(args[0])){
					parent.getFiles().readFile(arg);
					canReadLast = false;
				}
			}
		}
		if(parent.getFiles().exists(lastFile) && canReadLast)
			parent.getFiles().readFile(lastFile);
	}
	
	public void load()
	{
		Properties props = new Properties();
		profile = "default.profile";
		try {
			InputStream is = new FileInputStream("default.properties");
			props.load(is);
			profile = updateProps(props);
			
		} catch (FileNotFoundException e) {
			System.out.println("Error reading application properties. File not found.");
		} catch (IOException e) {
			System.out.println("Error reading application properties. IOException.");
		}
		try {
			InputStream is = new FileInputStream(profile);
			props.clear();
			props.load(is);
			updateOptions(props);
		} catch (FileNotFoundException e) {
			System.out.println("Error reading application properties. File not found. 2");
		} catch (IOException e) {
			System.out.println("Error reading application properties. IOException. 2");
		}
		
		// update dialog
		dialog.setScrollState(typewriterScroll);
		dialog.setSoundState(soundOn);
	}
	
	public void save()
	{
		Properties p = new Properties();
		p.put("fullscreen", parent.getWindowSys().isFull() + "" );
		if(parent.getWindowSys().isFull())
		{
			p.put("windowx", parent.getWindowSys().getBounds().getX() + "");
			p.put("windowy", parent.getWindowSys().getBounds().getY() + "");
			p.put("window.height", parent.getWindowSys().getBounds().getHeight() + "");
			p.put("window.width", parent.getWindowSys().getBounds().getWidth() + "");
		}
		else
		{
			p.put("windowx", parent.getX() + "");
			p.put("windowy", parent.getY() + "");
			p.put("window.height", parent.getHeight() + "");
			p.put("window.width", parent.getWidth() + "");
		}
		p.put("profile",profile);
		
		
		try {
			OutputStream os = new FileOutputStream("default.properties" );
			p.store(os,"Default Properties");
		} catch (FileNotFoundException e) {
			System.out.println("Error reading application properties. File not found.");
		} catch (IOException e) {
			System.out.println("Error reading application properties. IOException.");
		}
		
		p.clear();
		p.put("foreground","rgb(" + foreground.getRed()+","+foreground.getGreen()+","+foreground.getBlue()+")");
		p.put("background", "rgb(" + background.getRed()+","+background.getGreen()+","+background.getBlue()+")");
		p.put("font", font.getFamily() + " " + font.getStyle() + " " +font.getSize());
		p.put("hardcore",hardcore+"");
		p.put("sound",soundOn+"");
		p.put("typewriterscroll",typewriterScroll+"");
		p.put("lastfile", ""+parent.getEditor().getFilePath());
		
		try {
			OutputStream os = new FileOutputStream( profile );
			p.store(os,"User profile");
		} catch (FileNotFoundException e) {
			System.out.println("Error reading application properties. File not found. 2");
		} catch (IOException e) {
			System.out.println("Error reading application properties. IOException. 2");
		}
	}
	
	private void updateOptions(Properties p)
	{
		ColorFactory color = new ColorFactory();
		// hardcore
		hardcore = Boolean.parseBoolean(p.getProperty("hardcore","false"));
		// typewriter scroll
		typewriterScroll = Boolean.parseBoolean(p.getProperty("typewriterscroll","true"));
		// sound on
		soundOn = Boolean.parseBoolean(p.getProperty("sound","false"));
		// foreground
		String fg = p.getProperty("foreground","green");
		foreground = color.getColor(fg);
		/// background
		String bg = p.getProperty("background","black");
		background = color.getColor(bg);
		
		// loads lastfile
		lastFile = p.getProperty("lastfile","null");
		
		// font
		Scanner scan = new Scanner(p.getProperty("font","Courier plain 14"));
		String fname = scan.next();
		String style = scan.next();
		int size = scan.nextInt();
		int state = 0;
		if(style.equalsIgnoreCase("bold"))
			state = Font.BOLD;
		else if(style.equalsIgnoreCase("italic"))
			state = Font.ITALIC;
		else
			state = Font.PLAIN;
			
		font = new Font(fname,state,size);
	}
	
	private String updateProps(Properties p)
	{
		// full screen
		startFullScreen = Boolean.parseBoolean(p.getProperty("fullscreen", "true"));
		startW = (int)Double.parseDouble(p.getProperty("window.width", "600"));
		startH = (int)Double.parseDouble(p.getProperty("window.height", "400"));
		startX = (int)Double.parseDouble(p.getProperty("windowx", "10"));
		startY = (int)Double.parseDouble(p.getProperty("windowy", "10"));
		
		return p.getProperty("profile","default.profile");
	}
	
	public void disposeDialog()
	{
		dialog.disposeDialog();
	}
	public void disposeHelp()
	{
		help.disposeDialog();
	}
	
}



